package com.example.controller;

import com.example.common.Result;
import com.example.service.DeptService;
import com.example.entity.po.Dept;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author hongyi
 * @date 2024/8/24 16:30
 */
@RestController
@RequestMapping("/dept")
public class DeptController {


    @Resource
    private DeptService deptService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Dept dept) {
        deptService.insert(dept);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        deptService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        deptService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody Dept dept) {
        deptService.updateById(dept);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Dept dept = deptService.selectById(id);
        return Result.success(dept);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(Dept dept ) {
        List<Dept> list = deptService.selectAll(dept);
        return Result.success(list);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(Dept dept,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Dept> page = deptService.selectPage(dept, pageNum, pageSize);
        return Result.success(page);
    }

}
